from django.db import models


class User(models.Model):
    """ 用户表 """
    user_type = models.SmallIntegerField(verbose_name="用户类型", choices=((0, "注册用户"), (1, "VIP")), default=0)

    name = models.CharField(verbose_name="姓名", max_length=32)
    avatar = models.URLField(verbose_name="头像")
    open_id = models.CharField(verbose_name="微信OpenID", max_length=32, db_index=True)
    union_id = models.CharField(verbose_name="微信UnionID", max_length=32, db_index=True)

    create_date = models.DateTimeField(verbose_name="注册时间", auto_now_add=True)

    phone = models.CharField(verbose_name="手机号", max_length=32, null=True, blank=True)
    vip_date = models.DateTimeField(verbose_name="VIP时间", auto_now_add=True, null=True, blank=True)

    status = models.SmallIntegerField(verbose_name="用户状态", choices=((1, "正常"), (2, "拉黑")), default=1)

    class Meta:
        verbose_name_plural = "用户表"

    def __str__(self):
        return self.name


class Order(models.Model):
    """ 订单 """
    # 年月日
    date_ticket = models.CharField(verbose_name="下单日期", max_length=64, db_index=True)

    oid = models.CharField(verbose_name="订单ID", max_length=64, unique=True)
    pay_method = models.SmallIntegerField(verbose_name="支付方式", choices=((1, "支付宝"), (2, "微信")), default=1)
    amount = models.DecimalField(verbose_name="支付金额", max_digits=10, decimal_places=2)
    status_choices = ((1, "待支付"), (2, "已支付"), (3, "已取消"), (4, "申请退款"), (4, "已退款"))
    status = models.SmallIntegerField(verbose_name="订单状态", choices=status_choices, default=1)
    user = models.ForeignKey(verbose_name="用户", to="User", on_delete=models.CASCADE)
    pay_date = models.DateTimeField(verbose_name="支付时间", null=True, blank=True)

    class Meta:
        verbose_name_plural = "订单"


class Course(models.Model):
    """ 课程 """
    cover = models.FileField(verbose_name="封面", upload_to='cover')
    title = models.CharField(verbose_name="标题", max_length=32, db_index=True)
    sub_title = models.CharField(verbose_name="小标题", max_length=32)
    teacher = models.CharField(verbose_name="讲师", max_length=16)
    category_choices = (
        (1, "Python编程"),
        (2, "爬虫逆向"),
        (3, "安卓逆向"),
        (4, "自动化办公"),
        (5, "WEB开发"),
    )
    category = models.SmallIntegerField(verbose_name="分类", choices=category_choices)
    order = models.FloatField(verbose_name='排序', default=1)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "课程"


class Chapter(models.Model):
    """ 章节 """
    course = models.ForeignKey(verbose_name="课程", to="Course", on_delete=models.CASCADE)
    title = models.CharField(verbose_name="标题", max_length=32)
    order = models.FloatField(verbose_name='排序', default=1)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "章节"


class Video(models.Model):
    """ 视频 """
    course = models.ForeignKey(verbose_name="课程", to="Course", on_delete=models.CASCADE)
    chapter = models.ForeignKey(verbose_name="章节", to="Chapter", on_delete=models.CASCADE)
    vid = models.CharField(verbose_name="VID", max_length=32, db_index=True)
    title = models.CharField(verbose_name="标题", max_length=32)
    order = models.FloatField(verbose_name='排序', default=1)
    duration = models.IntegerField(verbose_name="时长", help_text="秒")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "视频"


class Coupon(models.Model):
    """ 优惠券（兑换码）"""
    status = models.SmallIntegerField(verbose_name="状态", choices=((0, "待使用"), (1, "已激活")), default=0)

    # 46c19a87-d014-40a4-8937-xdvadfadf
    code = models.CharField(verbose_name="兑换码", unique=True, max_length=36)

    user = models.ForeignKey(verbose_name="兑换用户", to="User", on_delete=models.CASCADE, null=True, blank=True)
    create_date = models.DateTimeField(verbose_name="注册时间", auto_now_add=True)

    memo = models.CharField(verbose_name="备注", max_length=32, null=True, blank=True)

    class Meta:
        verbose_name_plural = "优惠券"
